const { sqlite3 } = require('../../mysql')

async function indexAction(ctx) {
    // const openId = ctx.query.openId
    // 默认关键字
    const defaultKeyword = await sqlite3('nideshop_keywords').where({
        is_default: 1
    }).limit(1).select()
    // 取出热门关键字
    const hotKeywordList = await sqlite3('nideshop_keywords').distinct('keyword').column('keyword', 'is_hot').limit(10).select()
}

// 搜索时匹配搜索相关的内容
async function searchAction(ctx) {
    const keyword = ctx.query.keyword
    if (keyword) {
        const data = await sqlite3('scenic')
            .where('title', 'like', '%' + keyword + '%').select()
        if (data) {
            console.log('data:', data, 'total:', data.length)
            ctx.body = {
                code: 200,
                list: data,
                total: data.length,
                result: true,
            }
        } else {
            ctx.body = {
                code: false,
            }
        }
    }
}

// 搜索时匹配搜索相关的内容
async function articleSearch(ctx) {
    const keyword = ctx.query.keyword
    if (keyword) {
        const data = await sqlite3('article')
            .where('title', 'like', '%' + keyword + '%').select()
        if (data) {
            console.log('data:', data, 'total:', data.length)
            ctx.body = {
                code: 200,
                list: data,
                total: data.length,
                result: true,
            }
        } else {
            ctx.body = {
                code: false,
            }
        }
    }
}
module.exports = { indexAction, searchAction, articleSearch }
